package com.article.controller;

import com.article.common.PageParam;
import com.article.common.ResultBody;
import com.article.model.Article;
import com.article.model.User;
import com.article.service.IArticleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * @author lx
 */
@Controller
@RequestMapping("/wx/article")
public class WxArticleController {
	 private static final Logger log=LoggerFactory.getLogger(WxArticleController.class);
	 @Resource
	 private IArticleService service;

    @RequestMapping(value = "list",method = RequestMethod.GET)
    @ResponseBody
    public PageParam list(Integer page,Integer limit,String search,String id) {
        PageParam pageParam = new PageParam();
        pageParam.setLimit(limit);
        pageParam.setPage(page);
        Map<String,Object> map = new HashMap<>();
        map.put("search","");
        map.put("id","");
        if (search!=null&&!"".equals(search)){
            map.put("search",search);
        }
        if (id!=null&&!"".equals(id)){
            map.put("id",search);
        }
        pageParam.setParam(map);
        return this.service.queryAll(pageParam);
    }

    /**
     * 新增
     * @param entity
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public ResultBody add(Article entity){
        this.service.add(entity);
        return ResultBody.success("添加成功");
    }

    /**
     * 查找
     * @param id
     */
    @RequestMapping(value = "/getOne")
    @ResponseBody
    public ResultBody getOne(Integer id){
        Article obj=this.service.getOne(id);
        if(obj!=null){
            return ResultBody.success(obj);
        }else{
            return ResultBody.error("未获取到数据！");
        }
    }

    /**
     * 删除单条
     * @param id
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public ResultBody delete(Integer  id){
        this.service.delete(id);
        return ResultBody.success("删除成功");
    }

    /**
     * 删除多条
     * @param ids
     */
    @RequestMapping(value = "/deleteByIds",method = RequestMethod.POST)
    @ResponseBody
    public ResultBody deleteByIds(Integer[]  ids){
        this.service.deleteByIds(ids);
        return ResultBody.success("删除成功");
    }

    /**
     * 修改
     * @param entity
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public ResultBody update(Article entity){
        this.service.update(entity);
        return ResultBody.success("修改成功");
    }


}
